package com.ncs.nc.config;

import redis.clients.jedis.Jedis;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class JedisTest {
    public static void main(String[] args) {
        //连接本地Redis服务
        Jedis jedis=new Jedis("localhost");
        System.out.println("连接成功");
        //查看服务是否运行
        System.out.println("服务正在运行:"+jedis.ping("测试"));
        System.out.println("-------------------------------");

        //向Redis中添加String类型数据
        jedis.set("test01","test01_value");
        String str1=jedis.get("test01");
        System.out.println(str1);
        System.out.println("-------------------------------");

        //添加多个
        jedis.mset("test02","test02_value","test03","test03_value");
        List<String> mstr=jedis.mget("test02","test03");
        System.out.println(mstr);
        System.out.println("-------------------------------");

        //操作列表数据
        Jedis jedisName=new Jedis("localhost",6379);
        //存入起始位置
        jedisName.lpush("namelist","zhangsan");
        jedisName.lpush("namelist","李四");
        jedisName.lpush("namelist","wangwu");
        //获取范围成员遍历
        List<String> list=jedisName.lrange("namelist",0,10);
        for (String s : list) {
            System.out.println(s);
        }
        System.out.println("-------------------------------");

        //向Redis中添加Set类型数据，然后获取打印
        jedis.sadd("orders","jd001","jd002","jd003");
        Set<String> set1=jedis.smembers("orders");
        for (Iterator<String> iterator = set1.iterator();iterator.hasNext();) {
            String string=iterator.next();
            System.out.println(string);
        }
        System.out.println("-------------------------------");
    }
}
